﻿using System;
using System.IO;
using System.Web;
using XProject.DataVisit.Model;
using log4net;
using Newtonsoft.Json.Linq;
using System.Web;
using Newtonsoft.Json;

namespace XProject.DataVisit.Handler
{
    /// <summary>
    /// 
    /// </summary>
    public class JsonRequest : IJsonRequest
    {
        private JObject _requestObject;
        private JToken _requestParams;
        private HttpContext _context;
        private static ILog log = LogManager.GetLogger(typeof(JsonRequest));
        public static readonly string CATEGORY = "category";
        public static readonly string PARAMS = "params";
        private Category _category = Category.ReadOnly;

        public JsonRequest(HttpContext context)
        {
            _context = context;
            
            if (context == null)
                throw new ArgumentNullException("context", "context对象为空");

            string s = string.Empty;
            using (StreamReader reader = new StreamReader(context.Request.InputStream))
            {
                s = reader.ReadToEnd();
            }

            try
            {
                _requestObject = JObject.Parse(s);

                string cate = _requestObject.Value<string>(CATEGORY);

                _category = (Category)Enum.Parse(typeof(Category), cate, true);

                log.Debug("CATEGORY : " + _category);

                _requestParams = _requestObject.Value<JToken>(PARAMS);
                
                log.Debug("Params String " + _requestParams.ToString());
            }
            catch (Exception e)
            {
                log.Error(e);
                throw new Exception("请求格式不正确，非法的请求");
            }
        }

        public JsonRequest(string jsonData)
        {
            try
            {
                log.Debug("Request String : " + jsonData);
                _requestObject = JObject.Parse(jsonData);

                string cate = _requestObject.Value<string>(CATEGORY);

                _category = (Category)Enum.Parse(typeof(Category), cate, true);

                log.Debug("CATEGORY : " + _category);

                _requestParams = _requestObject.Value<JObject>(PARAMS);

                log.Debug("Params String " + _requestParams.ToString());
            }
            catch (Exception e)
            {
                log.Error(e);
                throw new Exception("请求格式不正确");
            }
        }

        public JToken Params
        {
            get
            {
                return _requestParams;
            }
        }

        public Category ActionCategory
        {
            get
            {
                return _category;
            }
        }

        public T GetParam<T>(string key)
        {
            var data = Params[key];

            return JavaScriptConvert.DeserializeObject<T>(data.ToString());
            //return Params.Value<T>(key);
        }

        public enum Category
        {
            Create,
            ReadOnly,
            Update,
            Delete,
            Insert
        }
    }
}
